package com.gao.baseboot.component;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.*;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 1.无参构造方法：new FooBean2()
 * 2.设置类属性
 * 3.执行XxxAware接口的实现方法(顺序见BeanFactory类)
 * 4.执行BeanPostProcessor的 postProcessBeforeInitialization()
 * 5.执行@PostConstruct方法
 * 6.执行InitializingBean接口的实现的afterPropertiesSet方法
 * 7.执行init-method
 * 8.执行BeanPostProcessor的 postProcessAfterInitialization()
 * <p>
 * //关闭容器
 * 1.执行DestructionAwareBeanPostProcessor的postProcessBeforeDestruction
 * 2.执行@PreDetroy
 * 3.执行DisposableBean的实现的destroy方法
 * 4.执行destroy-method方法
 *
 * @author GaoXi
 * @date 2022/2/20 19:07
 */
@Slf4j
public class FooBean2 implements InitializingBean, DisposableBean, BeanFactoryAware, ApplicationContextAware, ApplicationEventPublisherAware {


    public FooBean2() {
        log.info("构造函数执行");
    }


    @Autowired
    public FooBean fooBean;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("实现BeanFactoryAware接口");
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        log.info("实现ApplicationEventPublisherAware接口");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("实现ApplicationContextAware接口");
    }



    /*
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(beanName.equals("fooBean2")){
            log.info("postProcessBeforeInitialization");
        }
        return bean;
    }*/


    /**
     * init-method
     */
    @PostConstruct
    public void postConstruct() {
        log.info("@PostConstruct");
    }

    /**
     * 实现接口的一定在自有方法后面
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("实现InitializingBean接口");
    }

    /**
     * init-method
     */
    public void initMethod() {
        log.info("init-method方法");
    }


    /*
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(beanName.equals("fooBean2")) {
            log.info("postProcessAfterInitialization");
        }
        return bean;
    }*/


    /*
    @Override
    public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
        if(beanName.equals("fooBean2")){
            log.info("postProcessBeforeDestruction");
        }
    }*/

    /**
     *  @PreDestroy方法执行
     */
    @PreDestroy
    public void preDestroy() {
        log.info("@PreDestroy");
    }


    /**
     * 实现接口一定在自有方法后面
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        log.info("实现DisposableBean接口");
    }

    /**
     * destroyMethod
     */
    public void destroyMethod() {
        log.info("destroy-method方法");
    }
}
